<?php
/**
 * Created by PhpStorm.
 * User: dolia
 * Date: 2016/4/12
 * Time: 9:03
 */

namespace App\Http\Controllers\Api\V1;


use App\Exceptions\Api\ApiDoneErrorException;
use App\Exceptions\Api\ApiInputErrorException;
use App\Exceptions\Api\ApiRsaEecryptErrorException;
use App\Exceptions\Api\ApiUserNotLoginErrorException;
use App\Exceptions\Api\ApiValidateErrorException;
use App\Http\Controllers\Controller;
use App\Interfaces\CommentService;
use App\Models\OrderGoods;
use App\Http\Requests\Api\ApiRequest as Request;
use App\Models\Picture;

class CommentController extends Controller{

    /**
     * @api {post} /comment/create_comment 创建评论
     * @apiName Comment/createComment
     * @apiGroup Comment
     * @apiDescription 创建评论
     * @apiParam {int} order_goods_id 订单商品表ID
     * @apiParam {int} star 星级数
     * @apiParam {string} content 评论内容
     * @apiParam {string} type 是否匿名 1:匿名，0:不匿名
     * @apiParam {Object[]} [picture] 图片
     * @apiSuccess {Object[]} Object 评论信息
     * @apiSuccess {int} Object.comment_id 评论ID
     * @apiParamExample {json} Param-Example:
        {
        "order_goods_id" :217,
        "star":4,
        "content":"测试手机端评论",
        "type":"1",
        "picture":[
        "39b92687d342a78bdd73313b6b717b05.jpg",
        "6f007adceaa023ceb9714e617eaca5ff.jpg"
        ]
        }
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    {
    "comment_id": 26
    }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 403 Forbidden
    {
    "message": "请先登录"
    }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数order_goods_id"
    }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数star"
    }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数content"
    }
     * @apiErrorExample Error-Response 05:
     * HTTP/1.1 400 Bad Request
    {
    "message": "评论字数不能超过500字"
    }
     * @apiErrorExample Error-Response 06:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数type"
    }
     * @apiErrorExample Error-Response 07:
     * HTTP/1.1 422 Unprocessable Entity
    {
    "message": "该订单商品不存在"
    }
     * @apiErrorExample Error-Response 08:
     * HTTP/1.1 202 Accepted
    {
    "message": "该商品已评论"
    }
     * @apiErrorExample Error-Response 09:
     * HTTP/1.1 400 Bad Request
    {
    "message": "上传图片不能超过10张"
    }
     * @apiErrorExample Error-Response 10:
     * HTTP/1.1 202 Accepted
    {
    "message": "评论失败"
    }
     */
    public function createComment(Request $request,CommentService $commentService){
        $user_id = $request->apiUser->get()->id;
        $order_goods_id = $request->get('order_goods_id');
        $star = $request->get('star');
        $content = $request->get('content');
        $type = $request->get('type');
        //$orderGoods = OrderGoods::find($order_goods_id);

        if($user_id==''){
            throw new ApiUserNotLoginErrorException('请先登录');
        }
        if($order_goods_id==''){
            throw new ApiInputErrorException('缺少请求参数order_goods_id');
        }
        if($star==''){
            throw new ApiInputErrorException('缺少请求参数star');
        }
        if($content==''){
            throw new ApiInputErrorException('缺少请求参数content');
        }
        if(mb_strlen($content)>500){
            throw new ApiInputErrorException('评论字数不能超过500字');
        }
        if($type==''){
            throw new ApiInputErrorException('缺少请求参数type');
        }

        $orderGoods = OrderGoods::select('order_goods.*','order.order_id','user.user_name')
            ->join('order','order.order_id','=','order_goods.order_id')
            ->join('user','user.id','=','order.user_id')
            ->where('user.id',$user_id)
            ->where('order_goods.id',$order_goods_id)->get()->first();
        //dd($orderGoods);

        if(empty($orderGoods)){
            throw new ApiValidateErrorException('该订单商品不存在');
        }
        if($orderGoods->comment_status==1){
            throw new ApiDoneErrorException('该商品已评论');
        }

        $picStr="";

        if($request->has('picture')){
            $pictures = $request->get('picture');//dd($pictures);
            if(count($pictures)>10){
                throw new ApiInputErrorException('上传图片不能超过10张');
            }
            for($i=0;$i<count($pictures);$i++){

                //dd(explode(".",$pictures[$i])[0]);
                $id = explode(".",$pictures[$i])[0];
                $file_path = $pictures[$i];
                $file_ext = explode(".",$pictures[$i])[1];
                Picture::create([
                    'id'=>$id,
                    'file_path'=>$file_path,
                    'file_ext'=>$file_ext
                ]);

                if($picStr){
                    $picStr = $picStr.','.$id;
                }else{
                    $picStr = $id;
                }
            }
        }//dd($picStr);

        $comment_id = $commentService->createComment($user_id,$order_goods_id,$star,$content,$label=null,$type,$picStr);
        if($comment_id){
            return response()->json(['comment_id'=>$comment_id]);
        }else{
            throw new ApiDoneErrorException('评论失败');
        }

    }


} 